#include "BaseLog.hpp"
#include "TimeUnit.hpp"
#include "type.hpp"

#include <cstdarg>
#include <cstdio>
#include <sys/types.h>

level_t LEVEL_INFO = 0;
level_t LEVEL_WARN = 1;
level_t LEVEL_ERROR = 2;
level_t LEVEL_DEBUG = 4;

static char info[] =    "[info]  - ";
static char warn[] =    "[warn]  - ";
static char error[] =   "[error] - ";
static char debug[] =   "[debug] - ";

using Time = DBServer::Base::Timer::Time;
void db_printf(level_t level, const char *file, int line, const char *func, const char *fmt, ...)
{

    static const char format[] = "%s%s\n%s [%d] %s :\n";
    static Time timer;
    flockfile(stderr);

    if (level == LEVEL_INFO)
    {
        fprintf(stderr, "%s%s\n", info, timer.GET_CUR());
    }
    if (level == LEVEL_ERROR) {
        fprintf(stderr, format, error, timer.GET_CUR(), file, line, func);
    } else if (level == LEVEL_WARN) {
        fprintf(stderr, format, warn, timer.GET_CUR(), file, line, func);
    } else if (level == LEVEL_DEBUG) {
        fprintf(stderr, format, debug, timer.GET_CUR(), file, line, func);
    }

    va_list ap;
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);

    funlockfile(stderr);
}